package com.kungfuhacking.wristbandpro.my.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.base.view.HomeActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;

import io.reactivex.disposables.Disposable;

/**
 * 输入邀请码
 * Created by Aaron on 17/5/15.
 */

public class EntryCodeActivity extends BaseActivity implements View.OnClickListener {

    private EditText etEntryCode;
    private Disposable disposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_entry_code);
        initWidget();

    }

    private void initWidget() {
        TextView tvDone = (TextView) findViewById(R.id.tvDone);
        etEntryCode = (EditText) findViewById(R.id.etEntryCode);
        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);

        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvDone.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tvDone://完成

                if (TextUtils.isEmpty(etEntryCode.getText().toString())) {
                    showErr("请输入邀请码");
                    return;
                }

                bindOld();
                break;
        }
    }

    /**
     * 绑定老人
     */
    private void bindOld() {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("inviteKey", etEntryCode.getText().toString());
        RetrofitHttpUtil.getInstance().checkInviteKey(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                hideLoading();
                showErr("绑定成功");
                nextActivity(HomeActivity.class);
            }

            @Override
            public void onHandleError(String message) {
                hideLoading();
                showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                hideLoading();
                showErr(message);
                nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                disposable = d;
                showLoading("绑定中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (disposable != null)
            disposable.isDisposed();
    }
}
